package org.itboys.mobile.controller.web.product;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBook;
import org.itboys.mobile.entity.mongo.product.KitchenProductCookBook;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.web.product.KitchenProductCookBookService;
import org.itboys.mobile.service.mysql.web.product.SundayProductService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：农厨_商品_关联的食谱_controller_web
 *
 */
@Controller
@RequestMapping("/kitchen/web/product/cookbook")
public class KitchenProductCookBookController extends BaseController {

    @Autowired
    private KitchenProductCookBookService productCookBookService;

    /**
     * 商品-关联菜谱  主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                           Model model,
                         @RequestParam(value="productId",required=true)Integer productId){
        model.addAttribute("productId", productId);
       return "/kitchen/product/cookbook/index";
    }

    /**
     * 分页查询商品-关联菜谱
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                        HttpServletResponse response,
                              @RequestParam(value="productId",required=true)Long productId
    ) {
        try {
            PageResult<KitchenCookBook> result=productCookBookService.selectCookBook(request,productId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }




    /**
     * 新增商品-关联菜谱
     * @param request
     * @param response
     * @param productId
     * @param cookBookIds
     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @RequestParam(value="productId",required=true)Long productId,
                      @RequestParam(value="cookBookIds",required=true)List<Long> cookBookIds
    ) {
        try {
            productCookBookService.saveProductCookBook(productId,cookBookIds);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/kitchen/web/product/cookbook/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
    /**
     * 新增商品-关联菜谱
     * @param request
     * @param response
     * @param cookBookId
     */
    @RequestMapping("/delete")
    public  void delete(HttpServletRequest request,
                      HttpServletResponse response,
                      @RequestParam(value="cookBookId",required=true)Long cookBookId
    ) {
        try {
            productCookBookService.deleteProductCookBook(cookBookId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/kitchen/web/product/cookbook/delete------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }


}
